<script>
import { requestGet, requestPost } from '@/api/system/common'

export default {
  name: 'SubjectSetting',
  data() {
    return {
      dialog: false,
      from: {
        subId: null,
        ids: []
      },
      dataList: [],
      loading: false
    }
  },
  methods: {
    openSetting(data) {
      if (undefined === data || data === null) {
        this.$message.error('请选择服务主题')
      } else {
        this.dialog = true
        this.from.subId = data
        this.initAllService()
        this.initAllSubjectService()
      }
    },
    saveObject() {
      this.loading = true
      requestPost('/rest/api/manager/v2/service/subobj/save', this.from).then(() => {
        this.$message.success('保存成功！')
        this.beforeClose()
      }).finally(() => {
        this.loading = false
      })
    },
    initAllService() {
      requestGet('/rest/api/manager/service/all', {}).then((res) => {
        const { data } = res
        this.dataList = data
      }).catch(() => {
        this.dataList = []
      })
    },
    initAllSubjectService() {
      requestGet('/rest/api/manager/v2/service/subobj/query', { oid: this.from.subId })
        .then((res) => {
          const { data } = res
          this.from.ids = []
          if (data !== null && data.length > 0) {
            data.forEach(item => {
              this.from.ids.push(item.objId)
            })
          } else {
            this.from.ids = []
          }
        })
    },
    beforeClose() {
      this.from.subId = null
      this.from.ids = []
      this.dialog = false
    }
  }
}
</script>

<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :before-close="beforeClose"
    :visible.sync="dialog"
    title="服务主题设置"
    width="800px"
  >
    <div class="">
      <el-checkbox-group v-model="from.ids" size="small">
        <div class="custom-check-body">
          <div v-for="(item,index) in dataList" :key="'item'+index" class="custom-check-li">
            <el-checkbox :label="item.id" border>{{ item.name }}</el-checkbox>
          </div>
        </div>
      </el-checkbox-group>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="beforeClose()">取消</el-button>
      <el-button :loading="loading" type="primary" @click="saveObject">确认</el-button>
    </div>
  </el-dialog>
</template>

<style lang="scss" scoped>
.custom-check-body {
  width: 100%;
  height: auto;
  display: flex;
  flex-flow: row wrap;
  .custom-check-li {
    width: 20%;
    height: 50px;
  }
}
</style>
